package com.ruoyi.web.controller.liangshi;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.liangshi.domain.BiBsKscMonth;
import com.ruoyi.liangshi.service.IBiBsKscMonthService;
import com.ruoyi.liangshi.vo.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author LiuJin
 * @Date: Create in 2023-11-30 14:06
 * @Description: this class is
 */
@RestController
@RequestMapping("/linagshi/bs-ksc-month")
public class BiBsKscMonthController extends BaseController {

    @Autowired
    private IBiBsKscMonthService biBsKscMonthService;

    /**
     * 获取月份数据列表
     * @param shopId
     * @param date
     * @return
     */
    @GetMapping("/getBsKscMonthList")
    public TableDataInfo getBsKscHourList(@RequestParam(value = "shopId",required = false) Long shopId,@RequestParam(value = "date",required = false)String date) {
        startPage();
        List<BiBsKscMonthDataVo> biBsKscMonthDataVos = biBsKscMonthService.getBsKscMonthDataList( shopId, date);
        for (BiBsKscMonthDataVo bsKscMonthDataVo : biBsKscMonthDataVos) {
            Map<String, BigDecimal> itemTypeMaps = new HashMap<>();
            Map<String, BigDecimal> timeOutItemTypeMaps = new HashMap<>();
            List<BiBsKscMonth> kscMonthList = biBsKscMonthService.lambdaQuery()
                    .eq(BiBsKscMonth::getBsDate,bsKscMonthDataVo.getDate())
                    .list();
            //筛选菜品类型，然后对菜品数量进行类型
            for (BiBsKscMonth bsKscMonth : kscMonthList) {
                switch (bsKscMonth.getItemType()) {
                    case "A":
                        BigDecimal itemAcount = itemTypeMaps.get("A");
                        if(ObjectUtil.isNull(itemAcount)){
                            itemAcount=BigDecimal.ZERO;
                        }
                        itemAcount=itemAcount.add(bsKscMonth.getDishesCount());
                        itemTypeMaps.put("A",itemAcount);
                        if(bsKscMonth.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemAcount = timeOutItemTypeMaps.get("A");
                            if(ObjectUtil.isNull(timeOutItemAcount)){
                                timeOutItemAcount=BigDecimal.ZERO;
                            }
                            timeOutItemAcount=timeOutItemAcount.add(bsKscMonth.getDishesCount());
                            timeOutItemTypeMaps.put("A",timeOutItemAcount);
                        }
                        break;
                    case "B":
                        BigDecimal itemBcount = itemTypeMaps.get("B");
                        if(ObjectUtil.isNull(itemBcount)){
                            itemBcount=BigDecimal.ZERO;
                        }
                        itemBcount=itemBcount.add(bsKscMonth.getDishesCount());
                        itemTypeMaps.put("B",itemBcount);
                        if(bsKscMonth.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemBcount = timeOutItemTypeMaps.get("B");
                            if(ObjectUtil.isNull(timeOutItemBcount)){
                                timeOutItemBcount=BigDecimal.ZERO;
                            }
                            timeOutItemBcount=timeOutItemBcount.add(bsKscMonth.getDishesCount());
                            timeOutItemTypeMaps.put("B",timeOutItemBcount);
                        }
                        break;
                    case "C":
                        BigDecimal itemCcount = itemTypeMaps.get("C");
                        if(ObjectUtil.isNull(itemCcount)){
                            itemCcount=BigDecimal.ZERO;
                        }
                        itemCcount=itemCcount.add(bsKscMonth.getDishesCount());
                        itemTypeMaps.put("C",itemCcount);
                        if(bsKscMonth.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemCcount = timeOutItemTypeMaps.get("C");
                            if(ObjectUtil.isNull(timeOutItemCcount)){
                                timeOutItemCcount=BigDecimal.ZERO;
                            }
                            timeOutItemCcount=timeOutItemCcount.add(bsKscMonth.getDishesCount());
                            timeOutItemTypeMaps.put("C",timeOutItemCcount);
                        }
                        break;
                    default:
                        break;

                }
            }
            bsKscMonthDataVo.setItemTypeMaps(itemTypeMaps);
            bsKscMonthDataVo.setTimeOutItemTypeMaps(timeOutItemTypeMaps);
        }
        return getDataTable(biBsKscMonthDataVos);
    }

    /**
     * 获取月份出餐折线图数据
     * @param archType
     * @param date
     * @return
     */
    @GetMapping("/buildMonthDataChart")
    public AjaxResult buildMonthDataChart(@RequestParam(value = "archType", required = false) String archType,@RequestParam(required = false,value = "date")String date) {
        List<BiBsKscMonthChartData> bsKscMonthChartDataList = biBsKscMonthService.getBsKscMonthChartDataList(date);
        List<BigDecimal> aDishesCount = bsKscMonthChartDataList.stream().map(BiBsKscMonthChartData::getADishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> bDishesCount = bsKscMonthChartDataList.stream().map(BiBsKscMonthChartData::getBDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> cDishesCount = bsKscMonthChartDataList.stream().map(BiBsKscMonthChartData::getCDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> dishesTotalCount = bsKscMonthChartDataList.stream().map(BiBsKscMonthChartData::getDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> timeOutDishesCount = bsKscMonthChartDataList.stream().map(BiBsKscMonthChartData::getTimeOutDishesTotalCount).collect(Collectors.toList());
        List<String> monthData = bsKscMonthChartDataList.stream().map(BiBsKscMonthChartData::getMonth).collect(Collectors.toList());
        AjaxResult result = success();
        result.put("aDishesCount", aDishesCount);
        result.put("bDishesCount", bDishesCount);
        result.put("cDishesCount", cDishesCount);
        result.put("dishesTotalCount", dishesTotalCount);
        result.put("timeOutDishesCount", timeOutDishesCount);
        result.put("monthData", monthData);
        return result;
    }
    /**
     * 获取月份出餐耗时折线图数据
     * @param archType
     * @param date
     * @return
     */
    @GetMapping("/buildMonthAvgTimeDataChart")
    public AjaxResult buildMonthAvgTimeDataChart(@RequestParam(value = "archType", required = false) String archType,@RequestParam(required = false,value = "date")String date) {
        List<BiBsKscMonthAvgTimeChartData> bsKscMonthAvgTimeChartDataList = biBsKscMonthService.getBsKscMonthAvgTimeChartDataList(date);
        List<String> monthData = bsKscMonthAvgTimeChartDataList.stream().map(BiBsKscMonthAvgTimeChartData::getMonth).collect(Collectors.toList());
        List<BigDecimal> avgTotalTime = bsKscMonthAvgTimeChartDataList.stream().map(BiBsKscMonthAvgTimeChartData::getAvgTotalTime).collect(Collectors.toList());
        List<BigDecimal> avgATotalTime = bsKscMonthAvgTimeChartDataList.stream().map(BiBsKscMonthAvgTimeChartData::getAvgADishesTime).collect(Collectors.toList());
        List<BigDecimal> avgBTotalTime = bsKscMonthAvgTimeChartDataList.stream().map(BiBsKscMonthAvgTimeChartData::getAvgBDishesTime).collect(Collectors.toList());
        List<BigDecimal> avgCTotalTime = bsKscMonthAvgTimeChartDataList.stream().map(BiBsKscMonthAvgTimeChartData::getAvgCDishesTime).collect(Collectors.toList());
        AjaxResult result = success();
        result.put("monthData", monthData);
        result.put("avgTotalTime", avgTotalTime);
        result.put("avgATotalTime", avgATotalTime);
        result.put("avgBTotalTime", avgBTotalTime);
        result.put("avgCTotalTime", avgCTotalTime);
        return result;
    }
}
